<template>
  <div class="article">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item></el-breadcrumb
        >
      </div>
      <div class="text item">
        <el-form label-width="80px">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio :label="null">全部</el-radio>
              <el-radio :label="0">草稿</el-radio>
              <el-radio :label="1">待审核</el-radio>
              <el-radio :label="2">审核通过</el-radio>
              <el-radio :label="3">审核失败</el-radio>
              <el-radio :label="4">已删除</el-radio>
            </el-radio-group></el-form-item
          >
          <el-form-item label="频道">
            <!-- <el-select v-model="form.channel_id" placeholder="请选择">
              <el-option
                v-for="item in channelsList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select> -->
            <HmSelect :form="form"></HmSelect>
          </el-form-item>
          <el-form-item label="日期"
            ><el-date-picker
              v-model="form.picker"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker
          ></el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">筛选</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span v-show="total > 0" class="header">
          根据筛选条件查询到{{ total }}条数据</span
        >
      </div>
      <div class="text item">
        <el-table :data="list" stripe border>
          <el-table-column label="封面" width="150">
            <template v-slot="scope">
              <img :src="scope.row.cover.images[0]" alt="" class="cover" />
            </template>
          </el-table-column>
          <el-table-column label="标题" prop="title"> </el-table-column>
          <el-table-column label="状态" prop="status">
            <template v-slot="scope">
              {{ scope.row.status === 2 ? '审核通过' : '审核失败' }}
            </template>
          </el-table-column>
          <el-table-column label="发布时间" prop="pubdate"> </el-table-column>
          <el-table-column label="阅读数" prop="read_count"> </el-table-column>
          <el-table-column label="评论数" prop="comment_count">
          </el-table-column>
          <el-table-column label="点赞数" prop="like_count"> </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button type="text" @click="del(scope.row)">删除</el-button>
              <el-button type="primary" size="small" @click="edit(scope.row)"
                >编辑</el-button
              >
            </template></el-table-column
          >
        </el-table>
        <el-pagination
          @size-change="sizeChange"
          @current-change="pageCheng"
          :current-page="form.page"
          :page-sizes="[1, 5, 10, 20]"
          :page-size="form.per_page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { channels, mpArticles, mpArticlesDelete } from '@/api/index.js'
export default {
  data () {
    return {
      form: {
        picker: [],
        status: null,
        channel_id: '',
        begin_pubdate: '',
        end_pubdate: '',
        page: 1,
        per_page: 10
      },
      channelsList: [],
      list: [],
      total: 0
    }
  },
  // 侦听时间,时间数组改变就重新赋值开始和结束时间
  watch: {
    'form.picker': {
      handler (newVal, oldVal) {
        if (newVal.length === 2) {
          this.form.begin_pubdate = newVal[0]
          this.form.end_pubdate = newVal[1]
        }
      }
    },
    deep: true
  },
  // 获取列表
  async created () {
    const res = await channels()
    this.channelsList = res.data.channels
    this.getdata()
  },
  methods: {
    // 获取列表数据
    async getdata () {
      const res = await mpArticles(this.form)
      this.list = res.data.results
      this.total = res.data.total_count
    },
    // 点击筛选,刷新数据
    submit () {
      this.form.page = 1
      this.getdata()
    },
    // 编辑按钮,跳转编辑页面
    edit (row) {
      this.$router.push('/publish?id=' + row.id)
    },
    // 点击分页事件
    pageCheng (page) {
      this.form.page = page
      this.getdata()
    },
    // 设置分页量
    sizeChange (size) {
      this.form.per_page = size
      this.form.page = 1
      this.getdata()
    },
    // 删除事件
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await mpArticlesDelete(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.submit()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  text-align: center;
}
.cover {
  width: 100px;
  height: 100px;
}
</style>
